package pdfsetter.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import pdfsetter.core.XMLObjectSerializer;
import pdfsetter.core.schema.SimpleDocumentSchema;
import pdfsetter.core.schema.SinglePageSchema;
import pdfsetter.impl.pdfbox.compiler.PdfBoxDocumentCompiler;

public class GUI {
	private JFrame mainFrame;
	private final Dimension mainFrameDim = new Dimension(1000, 800);
	private PDFSchemaComponent schemaView;
	private SinglePageSchema schema;
	private PDFListPane pdfList;
	private JSplitPane splitPane;
	
	private void init() {
		pdfList = new PDFListPane(new JPanel());
		schemaView = new PDFSchemaComponent(null, pdfList);
		splitPane.setLeftComponent(pdfList);
		splitPane.setRightComponent(schemaView);
	}
	
	public GUI() {
		mainFrame = new JFrame("PDF Setter");
		mainFrame.setSize(mainFrameDim);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, null, null);
		mainFrame.add(splitPane);
		
		init();



		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("Opcje");
		JMenuItem addPDF = new JMenuItem("Dodaj dokument PDF");
		addPDF.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				int val = fc.showOpenDialog(mainFrame);
				if(val == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					try {
						pdfList.addPDF(file);
						pdfList.validate();
					} catch (FileNotFoundException e1) {
						JOptionPane.showMessageDialog(null, "Nie znaleziono pliku");
					} catch (IOException e1) {
						JOptionPane.showMessageDialog(null, "Niepoprawny plik PDF");
					}
				}
			}
		});

		JMenuItem selectSchema = new JMenuItem("Wybierz schemat");
		selectSchema.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser();
				int val = fc.showOpenDialog(mainFrame);
				if(val == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					try {
						schema = (SinglePageSchema) new XMLObjectSerializer().readObject(file.getPath());
						schemaView = new PDFSchemaComponent(schema, pdfList);
						splitPane.setRightComponent(schemaView);
					}
					catch(Exception e) {
						JOptionPane.showMessageDialog(null, "Niepoprawny plik schematu");
					}
				}
			}
		});


		JMenuItem savePDF = new JMenuItem("Zapisz");
		savePDF.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser();
				if(fc.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					new PdfBoxDocumentCompiler().compile(new SimpleDocumentSchema(schema, schemaView.getFillingSchema()), pdfList.getSourceFiles(), file);
				}
			}
		});
		
		JMenuItem reset = new JMenuItem("Reset");
		reset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				init();
			}
		});
		menu.add(addPDF);
		menu.add(selectSchema);
		menu.add(savePDF);
		menu.add(reset);
		menuBar.add(menu);
		mainFrame.setJMenuBar(menuBar);
	}

	public static void main(String[] args) {
		new GUI();
	}

}
